package com.iexbuy.ihg.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;

import com.iexbuy.ihg.App;
import com.iexbuy.ihg.R;
import com.iexbuy.ihg.entity.ChouJiangRecordListObject;
import com.iexbuy.ihg.net.ServiceHelper;
import com.iexbuy.ihg.utils.JsonUtils;
import com.iexbuy.ihg.utils.StringUtil;
import com.iexbuy.ihg.utils.ToastUtils;
import com.jingchen.pulltorefresh.PullToRefreshLayout;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class WeDeYunGouRecordDetailFragment extends BaseFragment {

	private static WeDeYunGouRecordDetailFragment homeFragmentInstent;

	@ViewInject(R.id.msgListLayout)
	private PullToRefreshLayout msgListRefreshLayout;
	@ViewInject(R.id.listView)
	private ListView listView;
	@ViewInject(R.id.itemInfoLayout)
	private LinearLayout itemInfoLayout;

	private LinearLayout listHeaderView;

	private static ChouJiangRecordListObject chouJiangRecordListObject;
	private List<ChouJiangRecordListObject.MyLuckNoObject> myLuckyNos = new ArrayList<ChouJiangRecordListObject.MyLuckNoObject>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		homeFragmentInstent = this;
	}

	@Override
	protected View _onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_wede_yunguo_record_detail, container, false);
		ViewUtils.inject(this, rootView);
		initView(rootView);
		return rootView;
	}

	private void initView(View rootView) {
		mBaseActivity.setTitle(getResources().getString(R.string.we_de_cashs_withdraw_record_detail));
		mBaseActivity.setTitleLayoutVisibility(true);
		mBaseActivity.setHomeActivity(false);

		listHeaderView = (LinearLayout) mBaseActivity.getLayoutInflater().from(mBaseActivity).inflate(R.layout.fragment_wede_yungou_record_detail_header, null);
		ViewUtils.inject(this, listHeaderView);
		listView.addHeaderView(listHeaderView);

		msgListRefreshLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
			@Override
			public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
				getDetailData();
			}

			@Override
			public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
				msgListRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
			}
		});
		msgListRefreshLayout.autoRefresh();
	}

	private void getDetailData() {
		HashMap<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("userId", App.sPreferences.getUserId());
		paramMap.put("period", String.valueOf(chouJiangRecordListObject.getPeriod()));
		paramMap.put("itemId", chouJiangRecordListObject.getItemId());
		ServiceHelper.send(mBaseActivity, ServiceHelper.GET_CHOU_JIAN_HISTORY_By_ITEMID_PERIOD, paramMap,
				new RequestCallBack<String>() {
					@Override
					public void onStart() {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.REFRESHING);
					}

					@Override
					public void onCancelled() {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.DONE);
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
						chouJiangRecordListObject = JsonUtils.parseJsonStrToObject(JsonUtils.getJsonSubStringByKey(responseInfo.result, "body"), ChouJiangRecordListObject.class);
						myLuckyNos.clear();
						myLuckyNos.addAll(chouJiangRecordListObject.getMyLuckyNo());
						initView();
					}

					@Override
					public void onFailure(HttpException error, String msg) {
						msgListRefreshLayout.refreshFinish(PullToRefreshLayout.FAIL);
						ToastUtils.toast(mBaseActivity, msg);
					}
				});
	}

	private void initView() {
		if (chouJiangRecordListObject != null) {
			initHeaderView();
			listView.setAdapter(new BaseAdapter() {
				@Override
				public int getCount() {
					return myLuckyNos.size();
				}
				@Override
				public Object getItem(int i) {
					return myLuckyNos.get(i);
				}
				@Override
				public long getItemId(int i) {
					return 0;
				}
				@Override
				public View getView(int i, View view, ViewGroup viewGroup) {
					if (view == null) {
						view = LayoutInflater.from(mBaseActivity).inflate(R.layout.fragment_wede_yunguo_record_detail_atten_item, null);
					}
					ChouJiangRecordListObject.MyLuckNoObject vo = myLuckyNos.get(i);
					((TextView) view.findViewById(R.id.itemName)).setText(vo.getLuckyNo());
					((TextView) view.findViewById(R.id.itemTime)).setText(vo.getTime());
					return view;
				}
			});
		}

	}

	private void initHeaderView(){

		View convertView = null;
		ChouJiangRecordListObject vo = chouJiangRecordListObject;
		if ("1".equalsIgnoreCase(chouJiangRecordListObject.getStatus())) {
			convertView = LayoutInflater.from(mBaseActivity).inflate(R.layout.fragment_yunguo_list_item, null);
			((TextView) convertView.findViewById(R.id.yungouName)).setText("[第"+vo.getPeriod()+"期] " + vo.getItemName());
			((TextView) convertView.findViewById(R.id.attendanceNo)).setText(vo.getAttendNumber());
			((TextView) convertView.findViewById(R.id.totalRequireNo)).setText(vo.getRequiredNumber());
			((TextView) convertView.findViewById(R.id.balenceNo)).setText(String.valueOf(Integer.valueOf(vo.getRequiredNumber()) - Integer.valueOf(vo.getAttendNumber())));
			((ProgressBar) convertView.findViewById(R.id.yunProgress)).setProgress(new BigDecimal(vo.getAttendNumber()).divide(new BigDecimal(vo.getRequiredNumber()), 2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal("100")).intValue());
			App.bitmapUtils.display((ImageView) convertView.findViewById(R.id.yungouImg), vo.getThumnailUrl());
		} else {
			convertView = LayoutInflater.from(mBaseActivity).inflate(R.layout.fragment_yunguo_list_item_finish, null);
			((TextView) convertView.findViewById(R.id.yungouName)).setText("[第"+vo.getPeriod()+"期] " + vo.getItemName());
			((TextView) convertView.findViewById(R.id.itemTime)).setText("开奖日期：" + vo.getRevealTime());
			((TextView) convertView.findViewById(R.id.luckerName)).setText("幸运儿： " + StringUtil.hideMobileNo(vo.getLuckUserName()));
			App.bitmapUtils.display((ImageView) convertView.findViewById(R.id.yungouImg), vo.getThumnailUrl());
		}
		itemInfoLayout.removeAllViews();
		itemInfoLayout.addView(convertView);
	}

	public static void setChouJiangRecordListObject(ChouJiangRecordListObject chouJiangRecordListObject) {
		WeDeYunGouRecordDetailFragment.chouJiangRecordListObject = chouJiangRecordListObject;
	}

	/**
	 * @return the homeFragmentInstent
	 */
	public static WeDeYunGouRecordDetailFragment getHomeFragmentInstent() {
		return homeFragmentInstent;
	}

}
